﻿namespace System.ServiceModel.DomainServices.Server
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Runtime.CompilerServices;

    internal static class TypeDescriptorExtensions
    {
        public static AttributeCollection Attributes(this Type type)
        {
            AttributeCollection attributes = TypeDescriptor.GetAttributes(type.BaseType);
            List<Attribute> list = new List<Attribute>(TypeDescriptor.GetAttributes(type).Cast<Attribute>());
        Label_008B:
            foreach (Attribute attribute in attributes)
            {
                AttributeUsageAttribute attribute2 = (AttributeUsageAttribute) TypeDescriptor.GetAttributes(attribute)[typeof(AttributeUsageAttribute)];
                if ((attribute2 != null) && !attribute2.Inherited)
                {
                    for (int i = list.Count - 1; i >= 0; i--)
                    {
                        if (object.ReferenceEquals(attribute, list[i]))
                        {
                            list.RemoveAt(i);
                            goto Label_008B;
                        }
                    }
                }
            }
            return new AttributeCollection(list.ToArray());
        }

        public static bool ContainsAttributeType<TAttribute>(this AttributeCollection attributes) where TAttribute: Attribute
        {
            return Enumerable.Any<Attribute>(attributes.Cast<Attribute>(), a => a.GetType() == typeof(TAttribute));
        }

        public static AttributeCollection ExplicitAttributes(this PropertyDescriptor propertyDescriptor)
        {
            List<Attribute> list = new List<Attribute>(propertyDescriptor.Attributes.Cast<Attribute>());
            AttributeCollection attributes = TypeDescriptor.GetAttributes(propertyDescriptor.PropertyType);
            bool flag = false;
            foreach (Attribute attribute in attributes)
            {
                for (int i = list.Count - 1; i >= 0; i--)
                {
                    if (object.ReferenceEquals(attribute, list[i]))
                    {
                        list.RemoveAt(i);
                        flag = true;
                    }
                }
            }
            if (!flag)
            {
                return propertyDescriptor.Attributes;
            }
            return new AttributeCollection(list.ToArray());
        }
    }
}

